package graph.io;

import graph.model.Graph;
import graph.model.Vertex;

import java.io.File;
import java.util.Scanner;

public class IOHandler {
	private Graph graph;
	
	public IOHandler( Graph graph ) {
		this.graph = graph;
	}
	
	public void readFile( String fileName ) {
		File file = new File( fileName );
		try {
			Scanner scanner = new Scanner( file );
			while( scanner.hasNextLine() ) {
				String[] vertexInfo = scanner.nextLine().split( " " );
				Vertex begin = graph.findCreateVertex( vertexInfo[0] );
				for( int index = 1; index < vertexInfo.length; index += 2 ) {
					graph.addEdge( begin, graph.findCreateVertex( vertexInfo[index] ), Integer.parseInt( vertexInfo[index + 1] ) );
				}
			}
		} catch( Exception e ) {
			e.printStackTrace();
		}
	}

}
